// pages/jiucanhuibaokiddetail/jiucanhuibaokiddetail.js
const util = require('../../utils/util.js')
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    startdate: util.formatDateMonth(new Date('2000-01-01')),
    enddate: util.formatDateMonth(new Date()),
    year: new Date().getFullYear(),
    month: new Date().getMonth() + 1,
    day: new Date().getDate(),
    currentdate: util.formatDate(new Date()),
    currentyear: new Date().getFullYear(),
    currentmonth: new Date().getMonth() + 1, // 月份
    currentday: new Date().getDate(),
    date: util.formatDate(new Date()),
    demo4_days_style: [],
    mid: null,
    kidid: null,
    ishalalfood: 0,
    ismealfee: 0,
    mealdays: 0,
    mealrocorddatelist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mid = options.mid
    var kidid = options.kidid
    var name = options.name
    this.setData({
      mid: parseInt(mid),
      kidid: parseInt(kidid),
    })
    wx.setNavigationBarTitle({
      title: "每日就餐记录" + name //页面标题为路由参数
    })
    console.log("mid===" + mid)
    console.log("kidid===" + kidid)

    this.queryData()
  },
  next: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData();

  },

  prev: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData();
  },


  queryData: function () {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "kidid": that.data.kidid,
      "mid": that.data.mid,
      "year": that.data.year,
      "month": that.data.month,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_mealrecordKid"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            ishalalfood: res.data.ishalalfood,
            ismealfee: res.data.ismealfee,
            mealdays: res.data.mealdays,
            mealrocorddatelist: res.data.mealrocorddatelist
          })
          that.setDateColor(that);
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },


  setDateColor: function (that) {
    const days_count = new Date(that.data.year, that.data.month, 0).getDate();
    let demo4_days_style = new Array;
    for (let i = 1; i <= days_count; i++) {
      var flag = 0;
      if (that.data.mealrocorddatelist != null) {
        for (let j = 0; j < that.data.mealrocorddatelist.length; j++) {
          var a = that.data.mealrocorddatelist[j].date;
          var days = parseInt(a.substring(8, 10));
          if (i == days) {
            flag = 1
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'black',
              background: 'yellow'
            });
            break;
          }
        }
      }
      if (flag == 0) {

        if (that.data.year == that.data.currentyear && that.data.month == that.data.currentmonth) {
          if (i == that.data.currentday) {
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'white',
              background: 'green'
            });
          }

        }

      }

    }
    that.setData({
      demo4_days_style: demo4_days_style
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})